package com.yaolisheng.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Describe:
 * Company: 神州数码信息系统有限公司
 * Version: V1.0
 * Author: lizbl
 * Date: 2022-11-02 10:10
 */
public class FileUtils {
    public static List<String> readTxtFileLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        String temp = null;
        List<String> ret = new ArrayList<String>();
        try {
            reader = new BufferedReader(new FileReader(file));
            while ((temp = reader.readLine()) != null) {
                ret.add(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public static String readTextFileAsResource(String path){
        StringBuffer sb = new StringBuffer();
        InputStream in = FileUtils.class.getClassLoader().getResourceAsStream(path);
        InputStreamReader read;
        try {
            read = new InputStreamReader(in, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                sb.append(lineTxt + "\r\n");
            }
            read.close();
            bufferedReader.close();
            sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
